package jp.ac.u_tokyo.dch.action.user;

import java.util.List;

import jp.ac.u_tokyo.dch.core.db.DBConnection;
import jp.ac.u_tokyo.dch.model.db.User;
import jp.ac.u_tokyo.dch.model.db.UserGroup;
import jp.ac.u_tokyo.dch.service.DaoServiceAware;
import jp.ac.u_tokyo.dch.service.Transaction;
import jp.ac.u_tokyo.dch.service.UserGroupService;
import jp.ac.u_tokyo.dch.util.DchCommonUtil;
import jp.ac.u_tokyo.dch.util.StringCheckUtil;
import jp.ac.u_tokyo.dch.util.common.ModoruUtil;
import jp.ac.u_tokyo.dch.view.Constants;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
/**
*
* @version 1.1
*
*/
public class UserGroupAction extends ActionSupport implements DaoServiceAware{

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * DBコネクションオブジェクト
	 */
	private DBConnection dbConnection = null;


	/** グループリスト */
	private List<UserGroup> groupList = null;

	private List<User> userList = null;

	private UserGroup userGroupView = null;

	private String editid;

	/**
	 * タイプ
	 */
	private String actionType;

	/**
	 * 初期化処理を行う
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String init() throws Exception {

		ModoruUtil.modoruProcess(ServletActionContext.getRequest(), "/users/group-init", ServletActionContext.getRequest().getQueryString());

		UserGroupService service = new UserGroupService(dbConnection);

		groupList = service.getList();

        return SUCCESS;
    }

	/**
	 * 登録
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String add() throws Exception {

		actionType = Constants.ADD;

		userGroupView = new UserGroup();

		String fUserId = DchCommonUtil.getLoginIdFromRequest(ServletActionContext.getRequest());
		String currentDate =DchCommonUtil.getCurrentDate();
		userGroupView.setCreateUser(fUserId);
		userGroupView.setCreateDate(currentDate);
		userGroupView.setUpdateUser(fUserId);
		userGroupView.setUpdateDate(currentDate);

		return SUCCESS;
	}

	/**
	 * 編集初期化
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String editInit() throws Exception {

		actionType = Constants.MOD;

		UserGroupService service = new UserGroupService(dbConnection);
		userGroupView = service.selectUserGroup(editid);
		userList = service.selectUserList(editid);
		String fUserId = DchCommonUtil.getLoginIdFromRequest(ServletActionContext.getRequest());
		String currentDate =DchCommonUtil.getCurrentDate();
		userGroupView.setUpdateUser(fUserId);
		userGroupView.setUpdateDate(currentDate);
		return SUCCESS;
	}
	/**
	 * 削除する
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String delInit() throws Exception {

		actionType = Constants.DEL;

		UserGroupService service = new UserGroupService(dbConnection);
		userGroupView = service.selectUserGroup(editid);
		userList = service.selectUserList(editid);
		return SUCCESS;
	}

	/**
	 * 戻る処理を行う
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String modoru() throws Exception {

		return SUCCESS;
	}

	/**
	 * 確認する処理を行う
	 * @return 結果
	 * @throws Exception 例外
	 */
	public String confirm() throws Exception {

		UserGroupService service = new UserGroupService(dbConnection);

		String id = userGroupView.getId();
		String name = userGroupView.getName();

		if (StringUtils.isEmpty(id)) {
			addFieldError("userGroupView.id", getText("requiredstring", new String[] { "ユーザグループID" }));
			return ERROR;
		}

		if (StringUtils.isEmpty(name)) {
			addFieldError("userGroupView.name", getText("requiredstring", new String[] { "ユーザグループ名" }));
			return ERROR;
		}

		if (StringCheckUtil.checkZenkaku(id)) {
			addFieldError("id", getText("hannkakuCheck", new String[] { "ユーザグループID" }));
			return ERROR;
		}

		if (Constants.ADD.equals(actionType) && service.isExistsId(id)) {

			addFieldError("id", getText("duplicateMsgArgs", new String[] { "ユーザグループID" }));
			return ERROR;
		}

		return SUCCESS;
	}

	/**
	 * 登録、削除、編集処理
	 * @return 結果
	 * @throws Exception 例外
	 */
	@Transaction
	public String execute() throws Exception {

		UserGroupService service = new UserGroupService(dbConnection);

		if (Constants.ADD.equals(actionType)) {
			service.insert(userGroupView);

		} else if (Constants.MOD.equals(actionType)) {
			service.update(userGroupView);

		} else if (Constants.DEL.equals(actionType)) {
			service.delete(userGroupView);
		}

		return SUCCESS;
	}


	@Override
	public void setDBConnection(DBConnection con) {
		dbConnection = con;

	}

	public List<UserGroup> getGroupList() {
		return groupList;
	}

	public void setGroupList(List<UserGroup> groupList) {
		this.groupList = groupList;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public String getActionType() {
		return actionType;
	}

	public void setActionType(String actionType) {
		this.actionType = actionType;
	}

	public UserGroup getUserGroupView() {
		return userGroupView;
	}

	public void setUserGroupView(UserGroup userGroupView) {
		this.userGroupView = userGroupView;
	}

	public String getEditid() {
		return editid;
	}

	public void setEditid(String editid) {
		this.editid = editid;
	}

}
